package com.vue.power.backend.sys.service;

import com.github.pagehelper.PageHelper;
import com.vue.power.backend.common.base.dao.BaseDao;
import com.vue.power.backend.common.base.service.BaseService;
import com.vue.power.backend.sys.dao.RoleAssociateTreeDao;
import com.vue.power.backend.sys.dao.RoleTreeDao;
import com.vue.power.backend.sys.dao.UserAssociateRoleDao;
import com.vue.power.backend.sys.dao.UserRoleDao;
import com.vue.power.backend.sys.dto.RoleTreeDto;
import com.vue.power.backend.sys.entity.RoleAssociateTree;
import com.vue.power.backend.sys.entity.RoleTree;
import com.vue.power.backend.sys.entity.UserAssociateRole;
import com.vue.power.backend.sys.entity.UserRole;
import com.vue.power.backend.sys.mapper.RoleTreeMapper;
import com.vue.power.backend.sys.util.TreeInstall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserRoleService extends BaseService<UserRole> {

    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private RoleTreeDao roleTreeDao;
    @Autowired
    private RoleAssociateTreeDao roleAssociateTreeDao;
    @Autowired
    private UserAssociateRoleDao userAssociateRoleDao;
    @Autowired
    private RoleTreeMapper roleTreeMapper;
    @Override
    protected BaseDao<UserRole> getDao() {
        return this.userRoleDao;
    }

    @Override
    public UserRole get(UserRole entity) {
        entity = super.get(entity);
        // 查询当前角色的已有的菜单数据
        List<RoleTree> roleTrees = roleTreeDao.queryRoleTree(entity);
        Map<String,Object> treeMap = new HashMap<>();
        RoleTree query = new RoleTree();
        query.setState("1");
        PageHelper.startPage(1, 9999);
        List<RoleTree> allTrees = roleTreeDao.select(query);
        for(RoleTree roleTree:roleTrees){
            treeMap.put(roleTree.getTreeId(),roleTree);
        }
        entity.setRoleTrees(TreeInstall.installCheckTree(roleTreeMapper.treesToTreeDTO(allTrees),treeMap));
        return entity;
    }

    @Override
    public boolean delete(UserRole entity) throws Exception {
        RoleAssociateTree roleAssociateTree = new RoleAssociateTree();
        roleAssociateTree.setRoleId(entity.getRoleId());
        roleAssociateTreeDao.delete(roleAssociateTree);
        UserAssociateRole userAssociateRole = new UserAssociateRole();
        userAssociateRole.setRoleId(entity.getRoleId());
        userAssociateRoleDao.delete(userAssociateRole);
        return super.delete(entity);
    }

    @Override
    public boolean save(UserRole entity) throws Exception {
        boolean isSuccess = super.save(entity);
        saveRoleAssociateTree(entity.getRoleTrees(),entity);
        return isSuccess;
    }

    /**
     * 功能描述：保存角色和菜单的关联关系的数据
     * @param roleTrees
     */
    private boolean saveRoleAssociateTree(List<RoleTreeDto> roleTrees,UserRole entity){
        boolean hasChildrenChecked = false;
        for(RoleTreeDto treeDto:roleTrees){
            if(treeDto.getChildren()!=null&&treeDto.getChildren().size()>0){
                if(saveRoleAssociateTree(treeDto.getChildren(),entity)||treeDto.isChecked()){
                    RoleAssociateTree roleAssociateTree = new RoleAssociateTree();
                    roleAssociateTree.setRoleId(entity.getRoleId());
                    roleAssociateTree.setTreeId(treeDto.getTreeId());
                    roleAssociateTreeDao.insert(roleAssociateTree);
                    hasChildrenChecked = true;
                }
            }else{
                if(treeDto.isChecked()){
                    RoleAssociateTree roleAssociateTree = new RoleAssociateTree();
                    roleAssociateTree.setRoleId(entity.getRoleId());
                    roleAssociateTree.setTreeId(treeDto.getTreeId());
                    roleAssociateTreeDao.insert(roleAssociateTree);
                    hasChildrenChecked = true;
                }
            }
        }
        return hasChildrenChecked;
    }
}
